<?php

namespace IDock\Insurance\CHAC\Response;

use IDock\Http\Response;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Visitor\NonCar\NonCarProductListVisitor;

class INonCarProductListResponse extends AllCHACResponse
{
    /*** @var NonCarProductListPrinter $printer */
    public $printer = NonCarProductListPrinter::class;

    /*** @var NonCarProductListVisitor $visitor */
    public $visitor;

    public $urlDecode = true;

    public function printerInit(): Response
    {
        $header = $this->get('head');

        if ($header['resCode'] != '0000') {
            $this->setError($header['resMsg'] ?? '');
            return $this;
        }

        $seats  =$this->visitor->seats;

        $body = $this->get('body',[]);

        $list = $body['suiteInfoList'] ?? [];

        foreach ($list as $suiteInfo) {
            $nonCarProductPrinter = new NonCarProductPrinter();

            $seatNums = $suiteInfo['seatNums'] ?? [];

            if (!in_array($seats,$seatNums)) {
                continue;
            }

            $nonCarProductPrinter->planCode = $suiteInfo['suiteCode'] ?? '';

            $nonCarProductPrinter->planName = $suiteInfo['suiteName'] ?? '';

            $nonCarProductPrinter->unitAmount = 0;

            $nonCarProductPrinter->argc['suiteSpecialClause'] = $suiteInfo['suiteSpecialClause'] ?? '';

            //总价
            if ($suiteInfo['suiteTotalPrice'] ?? '') {
                $nonCarProductPrinter->premium = $suiteInfo['suiteTotalPrice'] ?? '';
            }
            //单价
            if ($suiteInfo['unitPrice'] ?? '') {
                $nonCarProductPrinter->premium = $seats * $suiteInfo['unitPrice'];
            }

            //险种
            foreach ($suiteInfo['suiteInsList'] ?? [] as $coverage) {
                $nonCarSubCoveragePrinter = new NonCarSubCoveragePrinter();
                $nonCarSubCoveragePrinter->subCode = $coverage['coverageCode'] ?? '';
                $nonCarSubCoveragePrinter->subName = $coverage['coverageName'] ?? '';
                $nonCarSubCoveragePrinter->amount = $coverage['amount'] ?? '';

                if ($nonCarSubCoveragePrinter->amount) {
                    $nonCarProductPrinter->unitAmount += $nonCarSubCoveragePrinter->amount;
                }

                $nonCarProductPrinter->subCoverageAppend($nonCarSubCoveragePrinter);
            }

            $this->printer->productListAppend($nonCarProductPrinter);
        }

        return $this;
    }
}
